<template>
  <div>
    <!-- <div class="app-main-title">线下课程报名明细</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template #btns>
        <el-button
          v-if="meta.indexOf('CurriculumDetailExcel') > -1"
          :loading="exportLoading"
          type="primary"
          @click="batchexport"
        >{{ translate("export") }}</el-button>
      </template>
    </QueryForm>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('orderNum')"
        min-width="180"
        prop="orderId"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('userInfo')"
        min-width="200"
        show-overflow-tooltip
        prop="memberInfo"
      />
      <el-table-column
        align="center"
        :label="translate('courseName')"
        min-width="140"
        prop="courseName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('coursePriceUnit')"
        prop="allPrice"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('payMethod')"
        prop="payMethod"
        min-width="120"
        show-overflow-tooltip
      >
        <template scope="scope">
          <span>{{ nameWithPayMethod(scope.row.payMethod) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('orderStatus')"
        prop="orderStatus"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            scope.row.orderStatus == "0"
              ? translate("useStatus3")
              : scope.row.orderStatus == "1"
                ? translate("useStatus5")
                : scope.row.orderStatus == "2"
                  ? translate("useStatus6")
                  : scope.row.orderStatus == "3"
                    ? translate("zfcg")
                    : scope.row.orderStatus == "4"
                      ? translate("useStatus7")
                      : scope.row.orderStatus == "5"
                        ? translate("useStatus8")
                        : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('payTime')"
        prop="payTime"
        show-overflow-tooltip
        min-width="160"
      />
    </el-table>
    <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="pagination.page"
        hide-on-single-page
        @current-change="getList"
      />
    </div>
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { courseOrderVenueList, venueList, courseOrderVenueListExcel, allConfigList } from '@/api'
import { queryFormConfig } from './config'
export default {
  name: 'CurriculumDetail',
  data() {
    return {
      list: [],
      queryFormConfig,
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      exportLoading: false,
      payTypes: [],
      filters: {},
      listLoading: true,
      show: false,
      orderId: '',
      form: {},
      rules: {
        price: [
          { pattern: /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/, required: true, message: this.translate('qsrzqkszj'), trigger: 'blur' }
        ],
        info: [
          { required: true, message: this.translate('refundReason'), trigger: 'blue' }
        ]
      },
      meta: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getDefalutConfig()
  },
  methods: {
    nameWithPayMethod(payM) {
      const temp = this.payTypes.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    getVenueList() {
      venueList({
        param: { flag: '0' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          this.queryFormConfig.formItems[0].options = arr
          this.$set(this.filters, 'venueId', res.recordList[0].id)
          this.getList()
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    getDefalutConfig() {
      allConfigList({ param: { type: '2', code: '1' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.payTypes = res.recordList
          const arr = res.recordList
          this.queryFormConfig.formItems[3].options = arr
        })
        .catch((err) => {
          console.log(err)
        })
    },
    clearList() {
      this.filters = {
        venueId: this.filters.venueId
      }
      this.getList(1)
    },
    getList(page) {
      this.listLoading = false
      sessionStorage.setItem('venueId', this.filters.venueId)
      const { filters, pagination } = this
      const venueId = sessionStorage.getItem('venueId')
      filters.flag = '2'
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      pagination.firstQueryTime = parseTime(new Date())
      courseOrderVenueList({ pagination, param: { venueId, ...filters }}).then(res => {
        this.list = res.recordList
        this.total = res.totalCount
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    batchexport() {
      sessionStorage.setItem('venueId', this.filters.venueId)
      const { filters } = this
      filters.flag = '2'
      const venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      courseOrderVenueListExcel({ param: { venueId, ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>
